//
//  BSECGRootViewController.m
//  SuoSiApp
//
//  Created by 张明阳 on 16/4/21.
//  Copyright © 2016年 Ym. All rights reserved.
//
#import "AsyncSocket.h"
#import "BSECGRootViewController.h"

@interface BSECGRootViewController ()<UIScrollViewDelegate>
{
    AsyncSocket *_socket;
}
@end

@implementation BSECGRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BSWhite;

    [self addSubview];
   [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}


- (void)addSubview{
    //   蓝牙信号 和 电池电量
    //    第一条状态栏
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, -20, kScrenWidth, 20)];
    self.topView.backgroundColor = [UIColor colorWithRed:12.0/255 green:95.0/255 blue:35.0/255 alpha:1];
    [self.scrollView addSubview:_topView];
    
    //    蓝牙信号
    self.bluetoothSignLB = [[UILabel alloc]initWithFrame:CGRectMake(kMartin20, 0, kMartin80 - 10, kMartin20)];
    //    self.bluetoothSignLB.backgroundColor = BSRed;
    self.bluetoothSignLB.text = @"蓝牙信号";
    self.bluetoothSignLB.font = [UIFont systemFontOfSize:14];
    self.bluetoothSignLB.textColor = BSWhite;
    [self.topView addSubview:_bluetoothSignLB];
    
    //    蓝牙信号的图片
    self.RSSIImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.bluetoothSignLB.right, self.bluetoothSignLB.top, kMartin30, self.bluetoothSignLB.height)];
    self.RSSIImageView.image = [UIImage imageNamed:@"signalNo.png"];
    [self.topView addSubview:_RSSIImageView];
    
    //    电池电量
    self.batteryelectricLB = [[UILabel alloc]initWithFrame:CGRectMake(self.view.width - kMartin100 - kMartin50, 0, kMartin100 , kMartin20)];
//        self.batteryelectricLB.backgroundColor = BSRed;
    self.batteryelectricLB.text = @"电量剩余";
    self.batteryelectricLB.font = [UIFont systemFontOfSize:14];
    self.batteryelectricLB.textColor = BSWhite;
    [self.topView addSubview:_batteryelectricLB];
//    电池电量的图片
    
    self.electImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.batteryelectricLB.right - kMartin20, self.batteryelectricLB.top, kMartin30, self.bluetoothSignLB.height)];
//    self.electImageView.backgroundColor = BSBlue;
    [self.topView addSubview:_electImageView];

    
    
    
    
    
    //    导航
    self.navigationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, _topView.bottom, kScrenWidth, 44)];
    self.navigationView.userInteractionEnabled = YES;
//    self.navigationView.backgroundColor = BSGreen;
    [self.scrollView addSubview:_navigationView];
    //    返回按钮
    
    self.backBT = [self makeButtonWithframe:CGRectMake(kMartin15, kMartin10, kMartin30  , kMartin30) Title:nil];
    [self.backBT setBackgroundImage:[UIImage imageNamed:@"main_back_03.png"] forState:(UIControlStateNormal)];
    //    返回按钮
    [self.backBT addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    [self.navigationView addSubview:_backBT];
    //    人物标识
    self.iconBT = BSButton;
    self.iconBT.frame = CGRectMake(self.backBT.right + kMartin5, self.backBT.top, self.backBT.width - 5, self.backBT.height);
    [self.iconBT setBackgroundImage:[UIImage imageNamed:@"normal_title_contactshead"] forState:(UIControlStateNormal)];
    [self.navigationView addSubview:_iconBT];
    //    用户
    self.userLB = [[UILabel alloc]initWithFrame:CGRectMake(self.iconBT.right + kMartin5, self.iconBT.top, kScrenWidth - kMartin10 - self.iconBT.right, self.iconBT.height)];
    self.userLB.textColor = BSWhite;

    NSMutableAttributedString *str;
    if ([BSUserSGton userHandle].realName && [BSUserSGton userHandle].realName.length > 1) {
            str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@  欢迎您",[BSUserSGton userHandle].realName]];
    } else {
            str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@  欢迎您",[BSUserSGton userHandle].user_loginName]];
    }
    

    [str addAttribute:NSForegroundColorAttributeName value:BSBTGreen range:NSMakeRange(str.length - 3,3)];
    self.userLB.attributedText = str;
    [self.navigationView addSubview:_userLB];
    
    //    当前速度
    self.currentSpeedLB = [[UILabel alloc]initWithFrame:CGRectMake(kMartin20, self.navigationView.bottom  , kScrenWidth - 40, kMartin20)];
    self.currentSpeedLB.textColor = [UIColor greenColor];
    self.currentSpeedLB.text = @"当前速度: 10mm/mV 25mm/s";
    self.currentSpeedLB.font = [UIFont systemFontOfSize:13];
    [_scrollView addSubview:_currentSpeedLB];
    //    心跳
    self.heartBT = BSButton;
    self.heartBT.frame  =CGRectMake(kScrenWidth - 80, self.currentSpeedLB.top , kMartin20, kMartin20);
    [self.heartBT setBackgroundImage:[UIImage imageNamed:@"main_heat.png"] forState:(UIControlStateNormal)];
    [_scrollView addSubview:_heartBT];
    
    _heartLB = [[UILabel alloc]initWithFrame:CGRectMake(self.heartBT.right, self.heartBT.top, kMartin30, self.heartBT.height)];
    self.heartLB.textColor = BSRed;
    _heartLB.text = @"0";
    [_scrollView addSubview:_heartLB];
    
  
//        模式
    
    self.modeView = [[UIView alloc]init];
    
    if (kScrenWidth < 333) {
        

        _modeView.frame = CGRectMake(0, _Contentheight - 40 - 20, kScrenWidth, kMartin40 + 9);
        self.scrollView.contentSize = CGSizeMake(kScrenWidth, _modeView.bottom);

    } else {    //6以上的手机
        _modeView.frame = CGRectMake(0, _scrollView.height  - 40 - 20, kScrenWidth, kMartin40);
        self.scrollView.contentSize = CGSizeMake(kScrenWidth, _modeView.bottom);

    }
    
    
    
//    批量上传
    _modeView.backgroundColor = [UIColor colorWithRed:8.0/255.0 green:26.0/255.0 blue:33.0/255.0 alpha:1];
    [_scrollView addSubview:_modeView];
    
    self.batchBT = BSButton;
    
    _batchBT.frame = CGRectMake(kMartin15, kMartin10, kMartin80, _modeView.height - kMartin20);
    [_batchBT setTitle:@"批量模式" forState:(UIControlStateNormal)] ;
  
    [_modeView addSubview:_batchBT];

    
//    单通道
    
    self.threeeChannelBT = BSButton;
    self.threeeChannelBT.frame = CGRectMake(kScrenWidth  - _batchBT.right - kMartin30, self.batchBT.top , kMartin40 , _batchBT.height);
    [self.threeeChannelBT setBackgroundImage:[UIImage imageNamed:@"main_single_yes.png"] forState:(UIControlStateNormal)];
    [_modeView addSubview:_threeeChannelBT];
    
    
    _threeChannelLB = [[UILabel alloc]initWithFrame:CGRectMake(self.threeeChannelBT.right + kMartin10,0 - 1, _batchBT.width , _modeView.height )];
    _threeChannelLB.text = @"单通道";
    _threeChannelLB.font = [UIFont systemFontOfSize:18];
    _threeChannelLB.textColor = BSWhite;
    [_modeView addSubview:_threeChannelLB];
    
    self.scrollView .contentSize = CGSizeMake(kScrenWidth, _modeView.bottom );
    

    //    当前显示通道

    
    self.MIIBT = [self makeButtonWithframe:CGRectMake(kMartin10, self.modeView.top - kMartin20 - kMartin40, (kScrenWidth - kMartin10 * 4)/ 3, kMartin40) Title:@"MII"];
    
    [self.MIIBT addTarget:self action:@selector(channelTemp:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [_scrollView addSubview:_MIIBT];
    
    self.MV1BT = [self makeButtonWithframe:CGRectMake(self.MIIBT.left * 2 + _MIIBT.width, self.MIIBT.top, (kScrenWidth - self.MIIBT.left * 4)/ 3, self.MIIBT.height) Title:@"MV1"];
        [self.MV1BT addTarget:self action:@selector(channelTemp:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.MV1BT setBackgroundImage:[UIImage imageNamed:@"main_BT_bg.png"] forState:(UIControlStateSelected)];

    
    [_scrollView addSubview:_MV1BT];
    
    self.MV5BT = [self makeButtonWithframe:CGRectMake(self.MV1BT.right + self.MIIBT.left, self.MIIBT.top,self.MIIBT.width, self.MIIBT.height) Title: @"MV5" ];
        [self.MV5BT addTarget:self action:@selector(channelTemp:) forControlEvents:(UIControlEventTouchUpInside)];
    [_scrollView addSubview:_MV5BT];
    
    
    self.currentChannelLB = [self makeLableWithFrame:CGRectMake(self.MIIBT.left, self.MIIBT.top - kMartin50, kMartin100 + kMartin10, kMartin25) text:@"当前通道显示"];
    [_scrollView addSubview:_currentChannelLB];
    
    self.lineView = [[UIView alloc]initWithFrame:CGRectMake(self.currentChannelLB.left, _currentChannelLB.bottom + kMartin5, _currentChannelLB.width, 2)];
    _lineView.backgroundColor = BSBTGreen;
    [_scrollView addSubview:_lineView];
    
    

#pragma mark 当前速度调整
    
    self.speed5BT = [self makeButtonWithframe:CGRectMake(kMartin10, self.currentChannelLB.top - kMartin20 - kMartin40, (kScrenWidth - kMartin10 * 4)/ 3, kMartin40) Title:@"5mm/mV\n12.5mm/s"];
//    [self.speed5BT addTarget:self action:@selector(buttonClict:) forControlEvents:(UIControlEventTouchUpInside)];
//    self.speed5BT.backgroundColor = BSBTGreen;
    [self.speed5BT setBackgroundImage:[UIImage imageNamed:@"main_BT_bg.png"] forState:(UIControlStateSelected)];

    self.speed5BT.selected = NO;
    
    
    _speed5BT.titleLabel.font = [UIFont systemFontOfSize:14];
    [_scrollView addSubview:_speed5BT];
    
    
    self.speed10BT = [self makeButtonWithframe:CGRectMake(self.speed5BT.left * 2 + _speed5BT.width, self.speed5BT.top, (kScrenWidth - self.speed5BT.left * 4)/ 3, self.speed5BT.height) Title:@"10mm/mV\n25mm/s"];
// [self.speed10BT addTarget:self action:@selector(buttonClict:) forControlEvents:
//  (UIControlEventTouchUpInside)];
    _speed10BT.titleLabel.font = [UIFont systemFontOfSize:14];
    self.speed10BT.selected = NO;
    [self.speed10BT setBackgroundImage:[UIImage imageNamed:@"main_BT_bg.png"] forState:(UIControlStateSelected)];

    [_scrollView addSubview:_speed10BT];
    
    
    
    self.speed20BT = [self makeButtonWithframe:CGRectMake(self.speed10BT.right + self.speed5BT.left, self.speed5BT.top,self.speed5BT.width, self.speed5BT.height) Title: @"20mm/mV\n50mm/s" ];
//     [self.speed20BT addTarget:self action:@selector(buttonClict:) forControlEvents:(UIControlEventTouchUpInside)];
    self.speed20BT.selected = NO;
    [self.speed20BT setBackgroundImage:[UIImage imageNamed:@"main_BT_bg.png"] forState:(UIControlStateSelected)];

    _speed20BT.titleLabel.font = [UIFont systemFontOfSize:14];
    [_scrollView addSubview:_speed20BT];
    
    
    
    self.adjustSpeedLB = [self makeLableWithFrame:CGRectMake(self.speed5BT.left, self.speed5BT.top  - kMartin40, _currentChannelLB.width, _currentChannelLB.height) text:@"当前速度调整"];
    
    [_scrollView addSubview:_adjustSpeedLB];

    _lineView2 = [[UIView alloc]initWithFrame:CGRectMake(self.adjustSpeedLB.left, self.adjustSpeedLB.bottom + kMartin5, self.adjustSpeedLB.width, 2)];
    _lineView2.backgroundColor = BSBTGreen;
    [_scrollView addSubview:_lineView2];
}


- (void)addScrollViewWithheight:(CGFloat)height{
    
    _Contentheight = height;
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, kScrenWidth, kScrenHeight - 20)];

//    if (kScrenWidth > 360) { //6及6以上的手机
//            self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, kScrenWidth, kScrenHeight - 20)];
//    } else {
//            self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, kScrenWidth, kScrenHeight - 20)];
//    }
    

    
    _scrollView.delegate = self;
    _scrollView.bounces = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.backgroundColor = BSBlack;
    [self.view addSubview:_scrollView];
    
}


- (void)back{

//    NSTimer  会让taget的引用计数+1  只有invlidate  才能无效定时器 -1
   // [self.navigationController popViewControllerAnimated:YES];

}


- (UIButton *)makeButtonWithframe:(CGRect)frame Title:(NSString *)title{

    UIButton *button = BSButton;
    button.frame = frame;
    [button setTitle:title forState:(UIControlStateNormal)];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    button.titleLabel.numberOfLines = 0;
    button.backgroundColor = [UIColor colorWithRed:10.0/255 green:10.0/255 blue:10.0/255 alpha:1];
    return button;

}

- (UILabel *)makeLableWithFrame:(CGRect)frame text:(NSString *)text{

    UILabel *lable = [[UILabel alloc]initWithFrame:frame];
    lable.text = text;
    lable.textColor = BSBTGreen;
    return lable;
}

//- (void)buttonClict:(UIButton *)button{
//
//    
//    if (!_tempBT) {
//        
//        _tempBT = button;
//        button.selected = YES;
////        [button setBackgroundImage:[UIImage imageNamed:@"main_BT_bg.png"] forState:(UIControlStateNormal)];
//        
//    }else if([_tempBT isEqual:button]){
//
//    }else{
//
//        [_tempBT setBackgroundImage:nil forState:(UIControlStateNormal)];
//        [button setBackgroundImage:[UIImage imageNamed:@"main_BT_bg.png"] forState:(UIControlStateNormal)];
//        _tempBT = button;
//    }
//}

- (void)channelTemp:(UIButton *)button{
    if (button.selected == NO) {
        button.selected = !button.selected;
    }
            [button setBackgroundImage:[UIImage imageNamed:@"main_BT_bg.png"] forState:(UIControlStateSelected)];

    
//    if (!_channelTempBT) {
//        _channelTempBT = button;
//        [button setBackgroundImage:[UIImage imageNamed:@"main_BT_bg.png"] forState:(UIControlStateNormal)];
//        
//    }else if([_tempBT isEqual:button]){
//        
//        
//    }else{
//        
//        [_channelTempBT setBackgroundImage:nil forState:(UIControlStateNormal)];
//        [button setBackgroundImage:[UIImage imageNamed:@"main_BT_bg.png"] forState:(UIControlStateNormal)];
//        _channelTempBT = button;
//    }
}
- (void)setRSSIImage{
    
    NSLog(@"%ld",(long)[BSBluetoothSingleton shareBlutooth].RSSIValue);
    
//    设置蓝牙图片
    if ([BSBluetoothSingleton shareBlutooth].RSSIValue > 50) {
        //        信号强
        _RSSIImageView.image = [UIImage imageNamed:@"signalHigh.png"];
        
    } else if ([BSBluetoothSingleton shareBlutooth].RSSIValue < 30 && [BSBluetoothSingleton shareBlutooth].RSSIValue > 1) {
        //        信号弱
        _RSSIImageView.image = [UIImage imageNamed:@"signalLow.png"];
        
        [[BSBluetoothSingleton shareBlutooth] warningForSianalWeak];
        
    }else if ([BSBluetoothSingleton shareBlutooth].RSSIValue < 50 && [BSBluetoothSingleton shareBlutooth].RSSIValue > 30){
        //    信号一般般
        _RSSIImageView.image = [UIImage imageNamed:@"signalMiddle.png"];
        
    }else{
        _RSSIImageView.image = [UIImage imageNamed:@"signalNo.png"];
    }
    
//    设置电量
    NSLog(@"%f",[BSBluetoothSingleton shareBlutooth].batVolValue);
    
    //    设置电量图片
    if ([BSBluetoothSingleton shareBlutooth].batVolValue > 3.0) {
        //        电量强
        _electImageView.image = [UIImage imageNamed:@"electHigh.png"];
        
    } else if ([BSBluetoothSingleton shareBlutooth].batVolValue < 2.8 && [BSBluetoothSingleton shareBlutooth].batVolValue > 0) {
        //        电量弱
        _electImageView.image = [UIImage imageNamed:@"electLow.png"];
        [[BSBluetoothSingleton shareBlutooth] warningForElectricLow];

    }else if ([BSBluetoothSingleton shareBlutooth].batVolValue < 3.0 && [BSBluetoothSingleton shareBlutooth].batVolValue > 2.8){
        //    电量一般般
        _electImageView.image = [UIImage imageNamed:@"electMid.png"];
        
    }else{
        _electImageView.image = [UIImage imageNamed:@"common_blepower_0.png"];
        
    }
}
#pragma mark KVO 检测心率变化
- (void)observeHeartRate{

    BSBluetoothSingleton *bluSG = [BSBluetoothSingleton shareBlutooth];

       [bluSG addObserver:self forKeyPath:@"heartRate" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:@"haha"];
}
//  键值发生变化 会触发的方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
//    //哪一个属性发生的变化
//    NSLog(@"%@",keyPath);
//    //这个属性时属于谁的
//    NSLog(@"%@",object);
//    //change 值的变化
//    NSLog(@"%@",change);
//    //context 携带的参数
//    NSLog(@"%@",context);
    
    //    心率变化 更新心率
    self.heartLB.text = [NSString stringWithFormat:@"%@",change[@"new"]];
    //移除观察者
}

- (void)dealloc{
//    BSBluetoothSingleton *bluSG = [BSBluetoothSingleton shareBlutooth];
//    [bluSG removeObserver:self forKeyPath:@"heartRate"];
}

- (void)viewWillAppear:(BOOL)animated{
    
 [self setRSSIImage];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object
*/

@end
